/**
 * Project: webtest
 * Created: 2016年12月8日
 * Copyright ©2011-2016 gopay Information Technology Co., LTD All Rights Reserved
**/
package com.gopay.webtest.controller.cbrmb;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.util.BaseSign;
import com.gopay.webtest.util.DateUtils;
import com.gopay.webtest.vo.cbrmb.CbrmbFreightUploadInfo;
import com.gopay.webtest.xml.XmlDao;

/**
 * @ClassName: FreightUploadController
 * @author junjie.ge
 * @date 2016年12月8日 上午10:57:58
 */
@Controller
@RequestMapping("/cbrmb/freight")
public class CbrmbFreightController extends BaseController {
    private static final Logger LOG = LoggerFactory.getLogger(CbrmbFreightController.class);

    @Resource(name = "xmlDao")
    private XmlDao xmlDao;
    @Resource(name = "paySignHandler")
    private BaseSign paySignHandler;

    @RequestMapping("/jump2upload")
    public ModelAndView jump() {
        ModelAndView mv = new ModelAndView();

        try {
            // 读applicationContext.xml中的信息
            Map<String, String> map = xmlDao.readData4Map("cbrmbFreightUpload", "freightUpload");

            if (map.containsKey("gopayServerTime")) {
                map.remove("gopayServerTime");
                map.put("gopayServerTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            if (map.containsKey("tranDateTime")) {
                map.remove("tranDateTime");
                map.put("tranDateTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            }
            // 读取/webtest/webapp/WEB-INF/tiles-defs.xml定义的信息
            mv = new ModelAndView("cbrmb.freight.upload");
            mv.addObject("upload_prepare_info", map);
        } catch (Exception e) {
            LOG.error(e.getMessage(), new IllegalStateException(e));
        }

        return mv;
    }

    @RequestMapping("/upload")
    public ModelAndView submitUpload(CbrmbFreightUploadInfo info) {
        ModelAndView mav = new ModelAndView("cbrmb.freight.upload.submit");
        StopWatch sw = new StopWatch();

        // 设置MD5加密字段
        try {
            sw.start();
            paySignHandler.sign(info.getTranCode() + "_" + info.getVersion(), "signType", "signValue", info,getRequest());
            sw.stop();
            LOG.info("step1 time:{}", sw.getTime());
            LOG.info("商户密文值:{}", info.getSignValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mav.addObject("upload_submit_info", info);
        return mav;
    }
    
    @RequestMapping("/receive")
    public ModelAndView callbackMessage(HttpServletRequest request) {
        LOG.info("===========================接收通知请求详细参数==============================");
        @SuppressWarnings("unchecked")
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (String key : parameterMap.keySet()) {
            LOG.info("{} - {}", new Object[]{key, parameterMap.get(key)});
        }
        LOG.info("===========================参数详情显示完毕==============================");
        ModelAndView mv = new ModelAndView("cbrmb.freight.upload.callback");
        mv.addObject("xml", request.getParameter("xml"));
        return mv;
    }
}
